package com.android.newsreader;

import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
import android.util.Log;


/**
* Class for handling/parsing an RSS Feed
* 
* Sourced from:
*  http://www.ibm.com/developerworks/xml/tutorials/x-androidrss/section2.html
* 
*/
public class RSSHandler extends DefaultHandler 
{
	
	private RSSFeed mFeed;
	private RSSItem mItem;
	private String mLastElementName = "";
	private boolean mFoundChannel = false;
	
	private final int mRSS_TITLE = 1;
	private final int mRSS_LINK = 2;
	private final int mRSS_DESCRIPTION = 3;
	private final int mRSS_CATEGORY = 4;
	private final int mRSS_PUBDATE = 5;
	
	private int mDepth = 0;
	private int mCurrentstate = 0;
	
	/*
	 * Constructor 
	 */
	public RSSHandler()
	{
	}
	
	/*
	 * getFeed - this returns our feed when all of the parsing is complete
	 */
	public RSSFeed getFeed()
	{
		return mFeed;
	}
	
	
	public void startDocument() throws SAXException
	{
		// initialize our RSSFeed object - this will hold our parsed contents
		mFeed = new RSSFeed();
		// initialize the RSSItem object - we will use this as a crutch to grab the info from the channel
		// because the channel and items have very similar entries..
		mItem = new RSSItem();

	}
	
	public void endDocument() throws SAXException
	{
	}
	public void startElement(String namespaceURI, String localName,String qName, Attributes atts) throws SAXException
	{
		mDepth++;
		if (localName.equals("channel"))
		{
			mCurrentstate = 0;
			return;
		}
		if (localName.equals("image"))
		{
			// record our feed data - we temporarily stored it in the item :)
			mFeed.setTitle(mItem.getTitle());
			mFeed.setPubDate(mItem.getPubDate());
		}
		if (localName.equals("item"))
		{
			// create a new item
			mItem = new RSSItem();
			return;
		}
		if (localName.equals("title"))
		{
			mCurrentstate = mRSS_TITLE;
			return;
		}
		if (localName.equals("description"))
		{
			mCurrentstate = mRSS_DESCRIPTION;
			return;
		}
		if (localName.equals("link"))
		{
			mCurrentstate = mRSS_LINK;
			return;
		}
		if (localName.equals("category"))
		{
			mCurrentstate = mRSS_CATEGORY;
			return;
		}
		if (localName.equals("pubDate"))
		{
			mCurrentstate = mRSS_PUBDATE;
			return;
		}
		// if we don't explicitly handle the element, make sure we don't wind up erroneously 
		// storing a newline or other bogus data into one of our existing elements
		mCurrentstate = 0;
	}
	
	public void endElement(String namespaceURI, String localName, String qName) throws SAXException
	{
		mDepth--;
		if (localName.equals("item"))
		{
			// add our item to the list!
			mFeed.addItem(mItem);
			return;
		}
	}
	 
	public void characters(char ch[], int start, int length)
	{
		String theString = new String(ch,start,length);
		Log.i("RSSReader","characters[" + theString + "]");
		
		switch (mCurrentstate)
		{
			case mRSS_TITLE:
				mItem.setTitle(theString);
				mCurrentstate = 0;
				break;
			case mRSS_LINK:
				mItem.setLink(theString);
				mCurrentstate = 0;
				break;
			case mRSS_DESCRIPTION:
				mItem.setDescription(theString);
				mCurrentstate = 0;
				break;
			case mRSS_CATEGORY:
				mItem.setCategory(theString);
				mCurrentstate = 0;
				break;
			case mRSS_PUBDATE:
				mItem.setPubDate(theString);
				mCurrentstate = 0;
				break;
			default:
				return;
		}
		
	}
}
